package br.ufal.ic.dao.mock;

import br.ufal.ic.dao.UserDAO;
import br.ufal.ic.user.User;
import java.util.*;

/**
 *
 * @author Marcos Paulo
 */
public class MockUserDAO implements UserDAO {

    private Map<String, User> users;

    public MockUserDAO() {
        users = new HashMap<String, User>();
    }

    public MockUserDAO(Set<User> users) {
        this();
        initUsers(users);
    }

    private void initUsers(Set<User> users) {
        for (User user : users) {
            save(user);
        }
    }

    public User findByNickName(String nickName) {
        users.get(nickName);
        return users.get(nickName);
    }

    public void save(User user) {
        if (users.get(user.getNickName()) != null) {
            throw new RuntimeException("Usuario ja salvo na base de dado :"
                    + user);
        }
        users.put(user.getNickName(), user);
    }

    public void delete(User user) {
        users.remove(user.getNickName());
    }

    public void update(User user) {

        if (users.remove(user.getNickName()) == null) {
            throw new RuntimeException("Usuario não salvo : " + user);
        }

        save(user);
//        users.put(user.getNickName(), user);
    }

    public List<User> findAll() {
        return new ArrayList<User>(users.values());
    }

    public User findById(String id) {
        return users.get(id);
    }
}
